using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
//using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using Behemoth.SharePoint;

namespace Behemoth.SharePoint.LoggedCommentField {

  public class LoggedCommentFunctions {

    /*
    public static string GenerateHeaderString(LoggedCommentField fieldName) {
      return GenerateHeaderString(fieldName, DateTime.Now);
    }
    public static string GenerateHeaderString(LoggedCommentField fieldName, DateTime dt) {
      bool presenceAfter = fieldName.PresenceIconPlacement == PresenceIconPlacementChoice.AfterLink;
      string userPresence = SPPresenceTools.GetUserWithPresence(presenceAfter);
      return GenerateHeaderString(fieldName, userPresence, dt);
    }
    public static string GenerateHeaderString(LoggedCommentField fieldName, SPFieldUserValue user) {
      return GenerateHeaderString(fieldName, user, DateTime.Now);
    }
    public static string GenerateHeaderString(LoggedCommentField fieldName, SPFieldUserValue user, DateTime dt) {
      bool presenceAfter = fieldName.PresenceIconPlacement == PresenceIconPlacementChoice.AfterLink;
      string userPresence = SPPresenceTools.GetUserWithPresence(user, presenceAfter);
      return GenerateHeaderString(fieldName, userPresence, dt);
    }
    public static string GenerateHeaderString(LoggedCommentField fieldName, SPUser user) {
      return GenerateHeaderString(fieldName, user, DateTime.Now);
    }
    public static string GenerateHeaderString(LoggedCommentField fieldName, SPUser user, DateTime dt) {
      bool presenceAfter = fieldName.PresenceIconPlacement == PresenceIconPlacementChoice.AfterLink;
      string userPresence = SPPresenceTools.GetUserWithPresence(user, presenceAfter);
      return GenerateHeaderString(fieldName, userPresence, dt);
    }
    private static string GenerateHeaderString(LoggedCommentField fieldName, string userPresence) {
      return GenerateHeaderString(fieldName, userPresence, DateTime.Now);
    }
    private static string GenerateHeaderString(LoggedCommentField fieldName, string userPresence, DateTime dt) {
      string richTextFormat = fieldName.HeaderFormatRichText;
      string header = string.Format(richTextFormat, userPresence, dt.ToString());
      return header;
    }
    */


    /*    
    public static string GenerateHeaderString(string richTextFormat, PresenceIconPlacementChoice iconPlacement, string userName, DateTime dt) {
      SPUser user = SPContext.Current.Site.RootWeb.Users[userName];
      return GenerateHeaderString(richTextFormat, iconPlacement, user, dt);
    }
    public static string GenerateHeaderString(string richTextFormat, PresenceIconPlacementChoice iconPlacement, SPUser user, DateTime dt) {
      bool presenceAfter = iconPlacement == PresenceIconPlacementChoice.AfterLink;
      string userPresence = SPPresenceTools.GetUserWithPresence(user, presenceAfter);
      return GenerateHeaderString(richTextFormat, userPresence, dt);
    }
    public static string GenerateHeaderString(string richTextFormat, string userPresence, DateTime dt) {
      string header = string.Format(richTextFormat, userPresence, dt.ToString());
      return header;
    }
     * */

    /*
    public static bool IsTextFieldEmpty(TextBox textField) {
      string s = textField.Text;
      return IsTextFieldEmpty(s);
    }
    public static bool IsTextFieldEmpty(string text) {
      if (string.IsNullOrEmpty(text) || string.Compare(text, "<DIV></DIV>", true) == 0)
        return true;
      // Couldn't use XML document to get InnerText because the SharePoint API does
      // not return fully complioant XML: attributes don't have quotes.
      string xText = GetInnerText(text);
      xText = xText.Replace("&nbsp;", string.Empty);
      xText = xText.Trim();
      return (string.IsNullOrEmpty(xText));
    }

    /// <summary>
    /// Returns only the text that is not inside tags
    /// </summary>
    /// <remarks>
    /// There is probably some better way to improve this later with RegEx or something
    /// for example this will get really weird with non-richtext like "a &lt; b" or malformed 
    /// HTML like "&lt;div&gt;&gt;text&lt;/div&gt;", but for most cases this should get us the correct result.
    /// </remarks>
    /// <param name="xHtml"></param>
    /// <returns></returns>
    public static string GetInnerText(string xHtml) {
      string innerText = string.Empty; 
      bool inTag = false;
      foreach (char c in xHtml.ToCharArray()) {
        if (inTag && c == '>') {
          inTag = false;
        } else if (!inTag) {
          if (c == '<')
            inTag = true;
          else
            innerText += c.ToString();
        }
      }
      return innerText;
    }
     * */

    /*
    private static string GetTextField(LoggedCommentField fieldName, TextBox textField) {
      string text = textField.Text;
      return GetTextField(fieldName, text);
    }
    private static string GetTextField(LoggedCommentField fieldName, string text) {
      return GetTextField(fieldName.EditRichText, text);
    }
    private static string GetTextField(bool isEditRichText, string text) {
      if (!isEditRichText) {
        text = text.Replace("\n", "<br />");
      }
      text = "<div class=\"LoggedCommentText\">" + text + "</div>";
      return text;
    }
    */

    /*
    public static string GetNewFieldValue(LoggedCommentField fieldName, BaseFieldControl fieldControl, string text) {
      string s = string.Empty;
      if (fieldName.HistoryDisplaySortOrder == CommentHistoryOrder.RecentMessagesAtTop) {
        if (!IsTextFieldEmpty(text))
          s = fieldName.GenerateHeaderString() + GetTextField(fieldName, text);
        if (fieldControl.ItemFieldValue != null)
          s += fieldControl.ItemFieldValue.ToString();
      } else {
        if (fieldControl.ItemFieldValue != null)
          s = fieldControl.ItemFieldValue.ToString();
        if (!IsTextFieldEmpty(textField))
          s += fieldName.GenerateHeaderString() + GetTextField(fieldName, text);
      }
      return s;
    }
     */
    /* really need to prevent access to this until I can be sure there is no circular reference going on
    public static string AppendNewComment(LoggedCommentField fieldName, BaseFieldControl fieldControl, TextBox textField) {
      LoggedCommentFieldValue fv = fieldControl.ItemFieldValue as LoggedCommentFieldValue;
      fv.AppendNewComment(fieldControl.Web.CurrentUser, DateTime.Now, false, textField.Text);
      fv.SyncDisplayValues();
      //string currentText = (fieldControl.ItemFieldValue != null) ? fieldControl.ItemFieldValue.ToString() : string.Empty;
      //string s = AppendNewComment(fieldName, currentText, textField.Text);
      string s = fv.DisplayValueFull;
      return s;
    }
     * */

    /*
    public static string AppendNewComment(LoggedCommentField fieldName, SPUser user, DateTime dt, string currentText, string newText) {
      return AppendNewComment(fieldName, GenerateHeaderString(fieldName, user, dt), currentText, newText);
    }
    public static string AppendNewComment(LoggedCommentField fieldName, SPFieldUserValue user, DateTime dt, string currentText, string newText) {
      return AppendNewComment(fieldName, GenerateHeaderString(fieldName, user, dt), currentText, newText);
    }
    public static string AppendNewComment(LoggedCommentField fieldName, string currentText, string newText) {
      return AppendNewComment(fieldName, GenerateHeaderString(fieldName), currentText, newText);
    }
    public static string AppendNewComment(LoggedCommentField fieldName, string headerText, string currentText, string newText) {
      return AppendNewComment(fieldName.HistoryDisplaySortOrder, fieldName.EditRichText, headerText, currentText, newText);
    }
    public static string AppendNewComment(CommentHistoryOrder order, bool isEditRichText, string headerText, string currentText, string newText) {
      if (IsTextFieldEmpty(newText))
        return currentText;
      string fmt = (order == CommentHistoryOrder.RecentMessagesAtTop) ? "{0}{1}{2}" : "{2}{0}{1}";
      string s = string.Format(fmt, headerText, GetTextField(isEditRichText, newText), currentText);
      return s;
    }

    */

    public static TextBox SetupTextControl(LoggedCommentField field, BaseFieldControl fieldControl) {
      TextBox textField = fieldControl.TemplateContainer.FindControl("TextField") as TextBox;
      if (textField == null)
        throw new ArgumentException("textField is null. Corrupted ascx template file?");
      textField.TabIndex = fieldControl.TabIndex;
      textField.CssClass = fieldControl.CssClass;
      textField.ToolTip = field.Title + "";
      return textField;
    }

    public static HtmlInputHidden SetupHiddenControl(LoggedCommentField field, BaseFieldControl fieldControl) {
      if (!field.EditRichText)
        return null;
      HtmlInputHidden textField_spSave = fieldControl.TemplateContainer.FindControl("TextField_spSave") as HtmlInputHidden;
      if (textField_spSave == null)
        throw new ArgumentException("textField_spSave is null. Corrupted ascx template file?");
      return textField_spSave;
    }

    public static PlaceHolder AddCommentControl(LoggedCommentField field, BaseFieldControl fieldControl) {
      Literal commentText = new Literal();
      commentText.ID = "CommentHistoryText";

      // make this workwith teh strong type instead of a string
      LoggedCommentFieldValue fv = fieldControl.ItemFieldValue as LoggedCommentFieldValue;
      if (fv != null)
        fv.SyncDisplayValues();
      commentText.Text = (fieldControl.ItemFieldValue != null) ? fv.DisplayValueFull : string.Empty;

      PlaceHolder CommentHistoryPlaceHolder = new PlaceHolder();
      CommentHistoryPlaceHolder.ID = "CommentHistoryPlaceHolder";
      CommentHistoryPlaceHolder.Controls.Add(commentText);
      switch (field.HistoryDisplayPosition) {
        case CommentHistoryPosition.Top:
          fieldControl.TemplateContainer.Controls.AddAt(0, CommentHistoryPlaceHolder);
          break;
        case CommentHistoryPosition.Bottom:
          fieldControl.TemplateContainer.Controls.AddAt(fieldControl.TemplateContainer.Controls.Count, CommentHistoryPlaceHolder);
          break;
      }
      return CommentHistoryPlaceHolder;
    }

  } // class

} // namespace
